<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '分类添加' : '分类编辑'"
      :visible.sync="obj.isShow"
      width="30%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="上级分类">
          <el-select v-model="form.pid" placeholder="请选择商品分类">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option  v-for="item in CateList" :key="item.id" :label="item.catename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-input v-model="form.catename"></el-input>
        </el-form-item>
       <el-form-item label="图片">
    <div class="box">
        <h2>+</h2>
        <input type="file" @change="changeImg1" v-if="obj.isShow">
        <img :src="imgUrl" alt="" v-if="imgUrl">
    </div>
</el-form-item>
          <el-form-item label="状态">
    <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
  </el-form-item>

         </el-form>
      <span slot="footer" class="dialog-footer">
            <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="add()" v-if="obj.isAdd" >添加</el-button>
        <el-button type="primary" v-else @click="update()">编辑 </el-button >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {sucessAlert,warningAlert} from "../../../utils/alert.js"
import {cateAdd,cateInfo,cateEdit} from "../../../request/api.js"
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
        form:{
            pid:0,
            catename:"",
            img:"",
            status:1
        },
        imgUrl:""
    };
  },
  computed: {
    ...mapGetters({
        CateList:"cate/list"
    }),
  },
  methods: {
    ...mapActions({
        CateAsyncCateList:"cate/acyncChangeList"
    }),
    changeImg1(e) {
    // 图片的完整信息
    let file = e.target.files[0];
    console.log(file);
    // 验证图片大小限制
    // 1024B=1KB    1024KB=1MB   1024MB=1GB
    if (file.size > 2 * 1024 * 1024) {
        warningAlert("图片大小超出限制");
        return;
    }
    // 验证图片类型限制
    let imgType = [
        "image/jpeg",
        "image/jpg",
        "image/png",
        "image/webp",
        "image/gif",
    ];
    if (!imgType.includes(file.type)) {
        warningAlert("请上传规定类型图片");
        return;
    }
    // 根据图片的信息转换成具体的图片
    this.imgUrl = URL.createObjectURL(file);
    // 给form中的img字段赋值
    this.form.img = file;
},
    //清空
    empty(){
        this.form={
            pid:0,
            catename:"",
            img:"",
            status:1
        },
        this.imgUrl=""
    },
    cancel(){
        this.obj.isShow=false,
        this.empty()
    },
 //添加
    add(){
     cateAdd(this.form).then(res=>{
         console.log(res)
        if(res.data.code==200){
            sucessAlert(res.data.msg);
            this.cancel();
            this.CateAsyncCateList();
        }else{
            warningAlert(res.data.msg)
        }
     })
    // this.cancel()
    // sucessAlert()
 },
    //获取一条数据的值
    getOne(id){
        cateInfo({id:id}).then(res=>{
            if(res.data.code==200){
                this.form = res.data.list
                this.imgUrl = this.$pre + res.data.list.img
                this.form.id = id
            }
        })
    },
    //确认编辑
    update(){
         cateEdit(this.form).then(res=>{
            if(res.data.code==200){
                sucessAlert(res.data.msg),
                this.cancel()
                //刷新列表数据
                this.CateAsyncCateList()
  
            }
         })
    }
},
  mounted() {
      this.CateAsyncCateList()
  
}
}
</script>
<style scoped>
/* .box{
    width: 150px;
    height: 150px;
    border: 1px solid black;
    position: relative;
}
.box i {
    position: absolute;
    top:50%;
    left: 50%;
    transform: translate(-50%,-50%);
    font-size: 40px;
 
}
.box input{
    height: 150px;
    height: 150px;
    opacity: 0;
       z-index: 3;
}
.box img{
    width: 150px;
    height: 150px;
    position: absolute;
    top:0;
    left:0;
    z-index: 2;
} */
.box {
  width: 150px;
  height: 150px;
  border: 1px dashed #ccc;
  position: relative;
}
.box h2 {
  font-size: 50px;
  line-height: 150px;
  margin: 0 auto;
  text-align: center;
}
.box input {
  width: 150px;
  height: 150px;
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
  z-index: 3;
}
.box img {
  width: 150px;
  height: 150px;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
}
</style>